#!/bin/bash

#: 获取项目路径
basePath=$(cd $(dirname $0); pwd)
userId=$(id -u)

#: java启动项配置,请按自己的配置修改,谢谢!
javaParams="-Xms1024m -Xmx1024m -XX:ParallelGCThreads=4 -XX:MaxMetaspaceSize=128m -XX:MetaspaceSize=64m"
javaOpts="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$basePath/heapdump -verbose:gc -XX:+PrintGCDetails -XX:-PrintGCTimeStamps -XX:-UseParallelOldGC"
#: JVM监控配置,如果需要使用请修改监听端口,并且加入至java启动函数中
javaJVM="-Djava.rmi.server.hostname=主机ip -Dcom.sun.management.jmxremote.port=26004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"

#: 关闭应用进程
function stopProject(){
    appNameList=$(ls -t $basePath | grep '.jar$' | head -n 10)
    checkProject=true
    for appName in $appNameList; do
        appPrecess=$(ps -ef | grep $appName | grep -v grep | awk '{print $2}')
    if [ ! -z $appPrecess ]; then
            checkProject=false
            for precess in $appPrecess; do
                echo "项目名: $appName, 进程Id: $precess"
                appUserId=$(ps -ef | grep $precess | grep -v grep | awk '{print $1}')
                if (( $userId != 0 )) && (( $userId != $appUserId ));then
                    echo "项目名: $appName, 程序用户与系统用户不匹配，请检查应用进程!"
                    continue
                fi
        
                echo "项目名: $appName, 正在关闭进程..."
                kill $precess
                sleep 3
                if [ ! -z $(ps -e | grep $precess | awk '{print $1}') ];then
                    echo "项目名: $appName, 项目无法正常进行关闭,正在尝试强制关闭..."
                    kill -9 $precess
                    sleep 3
                fi
                if [ -z $(ps -e | grep $precess | awk '{print $1}') ];then
                    echo "项目名: $appName, 项目关闭完成"
                else
                    echo "项目名: $appName, 项目关闭失败,请自寻查找原因, 谢谢!"
                fi
            done
        fi
    done
    if $checkProject ; then
        echo "项目名: $(ls -t $basePath | grep '.jar$' | head -n 1), 项目并没有启动"
    fi
}

#: 启动应用程序
#: 启动jvm监听端口
function startProject(){
    #: 检查应用是否启动中     nohup java $javaParams $javaOpts $javaJVM
    appNameList=$(ls -t $basePath | grep '.jar$' | head -n 10)
    checkProject=false
    for appName in $appNameList; do
        appPrecess=$(ps -ef | grep $appName | grep -v grep | awk '{print $2}')
        if [ ! -z $appPrecess ];then
            checkProject=true
            echo "项目名: $appName, 进程Id: $appPrecess, 项目运行中"
        fi
    done
    if $checkProject ; then
       exit 0
    fi
    appName=$(ls -t $basePath | grep '.jar$' | head -n 1)
    echo "项目名: $appName, 开始启动中..."
    cd $basePath ; nohup java $javaParams $javaOpts -jar $basePath/$appName > /dev/null 2>&1 &
    sleep 5
    appPrecess=$(ps -ef | grep $appName | grep -v grep | awk '{print $2}')
    if [[ $appPrecess ]];then
        echo "项目名: $appName, 启动完成,PID: $appPrecess"
    else
        echo "项目名: $appName 启动失败,请查询日志,解决!"
    fi
}

#: 程序入口
case $1 in
    start)
        startProject
    ;;
    stop)
        stopProject
    ;;
    restart)
        stopProject
        startProject
    ;;
    *)
       echo "请输入: { start 或 stop 或 restart }"
    ;;
esac